package com.gxa.learning.pc.controller;

import com.gxa.learning.core.common.ExceptionCode;
import com.gxa.learning.core.common.JsonResult;
import com.gxa.learning.core.exception.BusinessException;
import com.gxa.learning.core.pojo.Course;
import com.gxa.learning.core.pojo.Project;
import com.gxa.learning.core.pojo.ProjectDetails;
import com.gxa.learning.core.pojo.Type;
import com.gxa.learning.pc.mapper.CourseMapper;
import com.gxa.learning.pc.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/project")
@Api(value = "和项目相关的api接口", tags = "项目相关接口")
public class ProjectController {
    @Autowired
    private ProjectService projectService = null;

    @Autowired
    private ProjectDetailsService projectDetailsService = null;

    @Autowired
    private ProjectCourseService projectCourseService = null;

    @Autowired
    private ProjectApplyService projectApplyService = null;

    @Autowired
    private ProjectSignService projectSignService = null;

    @Autowired
    private TestService testService = null;

    @Autowired
    private CourseMapper courseMapper = null;

    @Autowired
    private TypeService typeService = null;

    @Autowired
    private TrainPlanService trainPlanService = null;

    @ApiOperation(value = "获取全部的项目的简单信息列表")
    @GetMapping("/getList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "name", required = false, dataType = "String", paramType = "query", dataTypeClass = String.class),
            @ApiImplicitParam(name = "start", value = "start", required = false, dataType = "Date", paramType = "query", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "status", value = "status", required = false, dataType = "Integer", paramType = "query", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "man", value = "man", required = false, dataType = "String", paramType = "query", dataTypeClass = String.class),
            @ApiImplicitParam(name = "end", value = "end", required = false, dataType = "Date", paramType = "query", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "page", value = "page", required = false, dataType = "Integer", paramType = "query", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "size", value = "size", required = false, dataType = "Integer", paramType = "query", dataTypeClass = Integer.class)
    })
    public Map getProject(String name, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date start, Integer status, String man,
                          @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date end,
                          @RequestParam(name = "page", required = false) Integer page,
                          @RequestParam(name = "size", required = false) Integer size) throws IllegalAccessException, BusinessException {

        List<Project> byPage = projectService.getByPage(name, start, status, man, end, page, size);
        Integer byPageCount = projectService.getByPageCount(name, start, status, man, end);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(byPage);
        jsonResult.put("count", byPageCount);
        jsonResult.setCode("1");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }

    @ApiOperation("获取一个培训项目的详情")
    @ApiImplicitParam(name = "pid", value = "pid", required = true, dataType = "Long", paramType = "query", dataTypeClass = Long.class)
    @GetMapping("/getProjectDetails")
    public Map getProjectDetails(@RequestParam("pid") Long pid) throws BusinessException {
        ProjectDetails projectDetails = projectDetailsService.getProjectDetails(pid);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(projectDetails);
        jsonResult.setCode("1");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }


    @PostMapping("/save")
    @ApiOperation(value = "添加一个项目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectDetails", value = "projectDetails", required = true, dataType = "ProjectDetails", paramType = "body", dataTypeClass = ProjectDetails.class)
    })
    public Map addProject(@RequestBody ProjectDetails projectDetails) throws BusinessException {
        Date date = new Date();
        projectDetails.getProject().setProjectTime(date);
        projectDetailsService.addDetailsAndProject(projectDetails);
        System.out.println(projectDetails);
        System.out.println("projectDetails:" + projectDetails);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("成功");
        jsonResult.setData(projectDetails);
        return jsonResult.getDataMap();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改一个项目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectDetails", value = "projectDetails", required = true, dataType = "ProjectDetails", paramType = "body", dataTypeClass = ProjectDetails.class)
    })
    public Map updateProject(@RequestBody ProjectDetails projectDetails) throws BusinessException {
        System.out.println(projectDetails);
        projectDetailsService.updateDetailsAndProject(projectDetails);

        System.out.println("projectDetails:" + projectDetails);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("成功");
        jsonResult.setData(projectDetails);
        return jsonResult.getDataMap();
    }

    @PostMapping("/addApply")
    @ApiOperation(value = "添加一条申请记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "uid", required = true, dataType = "Long", paramType = "query", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "pid", value = "pid", required = true, dataType = "Long", paramType = "query", dataTypeClass = Long.class)
    })
    public Map addProjectApply(Long pid, Long uid) throws Exception {
        projectApplyService.projectApply(pid, uid);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("申请成功");
        return jsonResult.getDataMap();
    }

    @ApiOperation(value = "获取申请列表")
    @ApiImplicitParam(name = "pid", value = "pid", required = true, dataType = "Long", paramType = "query", dataTypeClass = Long.class)
    @GetMapping("/getApplyList")
    public Map<String, Object> getApplyList(@RequestParam("pid") Long pid, Integer page, Integer size) throws BusinessException {
        Integer count = projectApplyService.getAllApplyCount(pid);
        List<Map<String, Object>> allApply = projectApplyService.getAllApply(pid, page, size);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(allApply);
        jsonResult.put("count", count);
        return jsonResult.getDataMap();
    }

    @GetMapping("/getCourses")
    @ApiOperation(value = "获取选择的课程")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pid", value = "pid", required = true, dataType = "Long", paramType = "query", dataTypeClass = Long.class)
    })
    public Map<String, Object> getCourses(@RequestParam(name = "pid") Long pid) throws BusinessException {
        List<Course> courseList = projectCourseService.getCourseList(pid);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(courseList);
        return jsonResult.getDataMap();
    }

    @GetMapping("/getCourseList")
    @ApiOperation(value = "返回所有的课程")
    public Map getCourseList() {
        List<Course> courses = courseMapper.getAllCourse();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(courses);
        return jsonResult.getDataMap();
    }

    @ApiOperation(value = "给一个项目添加课程")
    @ApiImplicitParam(name = "map", value = "map", paramType = "body")
    @PostMapping("/addCourseToProject")
    public Map addCourseToProject(@RequestBody Map<String, Object> map) throws BusinessException {

        System.out.println(map);
        Object pid = map.get("pid");
        Integer i = Integer.parseInt(pid.toString());
        System.out.println(i);
        Long thePid = Long.valueOf(i.toString());

        List<Integer> thecids = (List<Integer>) map.get("cids");

        ArrayList<Long> ids = new ArrayList<>();
        for (Integer id : thecids) {
            ids.add(Long.valueOf(id.toString()));
        }
        projectCourseService.addProjectCourse(ids, thePid);
        System.out.println(thecids);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("操作成功");
        return jsonResult.getDataMap();
    }

    @ApiOperation(value = "获取学员列表")
    @GetMapping("/getStudents")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "name", required = false, dataType = "String", paramType = "query", dataTypeClass = String.class),
            @ApiImplicitParam(name = "company", value = "company", required = false, dataType = "String", paramType = "query", dataTypeClass = String.class),
            @ApiImplicitParam(name = "position", value = "position", required = false, dataType = "String", paramType = "query", dataTypeClass = String.class),
            @ApiImplicitParam(name = "page", value = "page", required = false, dataType = "Integer", paramType = "query", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "size", value = "sizes", required = false, dataType = "Integer", paramType = "query", dataTypeClass = Integer.class)
    })
    public Map getStudents(@RequestParam(name = "pid") Long pid, String name, String company, String position, Integer page, Integer size) throws BusinessException {
        int count = projectApplyService.getStudentsCount(pid, name, company, position);
        List<Map<String, Object>> students = projectApplyService.getStudents(pid, name, company, position, page, size);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(students);
        jsonResult.put("count", count);
        return jsonResult.getDataMap();
    }

    @ApiOperation(value = "删除一个学员信息")
    @GetMapping("/delStudent")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "body", dataTypeClass = Long.class)
    public Map delStudent(@RequestParam("id") Long id) throws BusinessException {
        projectApplyService.delStudent(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }

    @ApiOperation(value = "批量修改申请的状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "applyId", value = "applyId", required = true, dataType = "Long", paramType = "body", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "status", value = "status", required = true, dataType = "Integer", paramType = "query", dataTypeClass = Integer.class)
    })
    @PostMapping("/setApplyStatus")
    public Map setApplyStatus(@RequestBody Map<String, Object> map) throws Exception {
        List<Integer> applyIds = (List<Integer>) map.get("applyIds");
        if (applyIds == null) {
            throw new BusinessException(ExceptionCode.ARGUMENTS_ERROR);
        }
        ArrayList<Long> list = new ArrayList<>();
        for (Integer applyId : applyIds) {
            list.add(Long.valueOf(applyId));
        }
        Integer st = (Integer) map.get("status");
        Integer integer = projectApplyService.setApplyStatus(list, st);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("修改成功,共修改了" + integer + "条记录！");
        return jsonResult.getDataMap();
    }

    @ApiOperation(value = "发起签到")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pid", value = "pid", required = true, dataType = "Long", paramType = "query", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "uid", value = "uid", required = true, dataType = "Long", paramType = "query", dataTypeClass = Long.class)
    })
    @GetMapping("/sendSignIn")
    public Map<String, Object> signIn(@RequestParam(name = "pid") Long pid, @RequestParam(name = "uid") Long uid) throws BusinessException {
        projectSignService.sendSignIn(pid, uid);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }

    @GetMapping("/getSignList")
    @ApiOperation(value = "获取签到记录的列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "name", required = false, dataType = "String", paramType = "query", dataTypeClass = String.class),
            @ApiImplicitParam(name = "page", value = "page", required = false, dataType = "Integer", paramType = "query", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "size", value = "size", required = false, dataType = "Integer", paramType = "query", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pid", value = "pid", required = true, dataType = "Long", paramType = "query", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "start", value = "start", dataType = "Date", paramType = "query", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "end", value = "end", dataType = "Date", paramType = "query", dataTypeClass = Date.class)
    })
    public Map getSignList(String name, Integer page, Integer size, Long pid,
                           @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date start,
                           @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date end) throws BusinessException {
        List<Map<String, Object>> signInList = projectSignService.getSignInList(name, page, size, pid, start, end);
        int count = projectSignService.getAllSignInList(name, pid).size();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("成功");
        jsonResult.setData(signInList);
        jsonResult.put("count", count);
        return jsonResult.getDataMap();
    }


    @ApiOperation("停止签到")
    @GetMapping("/stopSign")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pid", value = "pid", required = true, dataType = "Long", paramType = "query", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "uid", value = "uid", required = true, dataType = "Long", paramType = "query", dataTypeClass = Long.class)
    })
    public Map<String, Object> stopSign(@RequestParam(name = "pid") Long pid, @RequestParam(name = "uid") Long uid) throws BusinessException {
        projectSignService.stopSign(pid, uid);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }

    @ApiOperation("判断当前用户是否发起了签到")
    @GetMapping("/getSignStatus")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pid", value = "pid", required = true, dataType = "Long", paramType = "query", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "uid", value = "uid", required = true, dataType = "Long", paramType = "query", dataTypeClass = Long.class)
    })
    public Map<String, Object> getSignStatus(@RequestParam(name = "pid") Long pid, @RequestParam(name = "uid") Long uid) throws BusinessException {
        Boolean isSend = projectSignService.isSendSign(uid, pid);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.put("status", isSend);
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }


    @ApiOperation(value = "判断当前的用户是否有签到")
    @GetMapping("/haveCheckIn")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pid", value = "pid", required = true, paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "uid", value = "uid", required = true, paramType = "query", dataType = "Long", dataTypeClass = Long.class)
    })
    public Map<String, Object> haveCheckIn(@RequestParam(name = "pid") Long pid, @RequestParam(name = "uid") Long uid) throws BusinessException {
        String s = projectSignService.haveCheckSign(pid, uid);
        JsonResult jsonResult = new JsonResult();
        jsonResult.put("key", s);
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @ApiOperation(value = "学员签到")
    @GetMapping("/signIn")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pid", value = "pid", required = true, dataType = "Long", paramType = "query", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "uid", value = "uid", required = true, dataType = "Long", paramType = "query", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "key", value = "key", required = true, dataType = "String", paramType = "query", dataTypeClass = String.class)
    })
    public Map<String, Object> signIn(Long pid, Long uid, String key) throws BusinessException {
        JsonResult jsonResult = new JsonResult();
        projectSignService.signIn(pid, uid, key);
        jsonResult.setCode("1");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }

    @ApiOperation("获取学员自己的签到记录")
    @GetMapping("/getMySignList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pid", value = "pid", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "uid", value = "uid", required = true, paramType = "query", dataType = "Long")
    })
    public Map<String, Object> getMySignList(@RequestParam("pid") Long pid, @RequestParam("uid") Long uid) throws BusinessException {
        List<Map<String, Object>> mySignList = projectSignService.getMySignList(pid, uid);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("成功");
        jsonResult.setData(mySignList);
        return jsonResult.getDataMap();
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "qid", value = "qid", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "pid", value = "pid", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "passScore", value = "passScore", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "testTime", value = "testTime", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "endDay", value = "endDay", dataType = "Integer", dataTypeClass = Integer.class)
    })
    @ApiOperation(value = "发起考试")
    @GetMapping("/sendTest")
    public Map sendTest(@RequestParam(name = "qid") Long qid, @RequestParam(name = "pid") Long pid, @RequestParam(name = "passScore") Integer passScore,
                        @RequestParam(name = "testTime") Integer testTime, @RequestParam(name = "endDay") Integer endDay) throws BusinessException {
        testService.sendTest(qid, pid, passScore, testTime, endDay);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }

    @ApiOperation(value = "获取所有的成绩列表")
    @GetMapping("/getScoreList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pid", value = "pid", required = true, dataType = "Long", paramType = "query", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "name", value = "name", required = false, dataType = "String", paramType = "query", dataTypeClass = String.class),
            @ApiImplicitParam(name = "company", value = "company", required = false, dataType = "String", paramType = "query", dataTypeClass = String.class),
            @ApiImplicitParam(name = "department", value = "department", required = false, dataType = "String", paramType = "query", dataTypeClass = String.class),
            @ApiImplicitParam(name = "page", value = "page", required = false, dataType = "Integer", paramType = "query", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "size", value = "size", required = false, dataType = "Integer", paramType = "query", dataTypeClass = Integer.class)
    })
    public Map getScoreList(@RequestParam(name = "pid") Long pid, String name, String company, String department, Integer page, Integer size) throws BusinessException {
        List<Map<String, Object>> scoreList = testService.getScoreList(pid, name, company, department, page, size);
        int count = testService.getAllScoreList(pid, name, company, department).size();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(scoreList);
        jsonResult.put("count", count);
        jsonResult.setCode("1");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }

    @ApiOperation(value = "获取所有的类别")
    @GetMapping("/getTypes")
    public Map<String, Object> getType() {
        List<Type> types = typeService.selectAll();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("成功");
        jsonResult.setData(types);
        return jsonResult.getDataMap();
    }

    @ApiOperation(value = "获取所有计划")
    @GetMapping("/getPlans")
    public Map<String, Object> getPlans() throws BusinessException {
        List<Map<String, Object>> data = projectDetailsService.getTypeBy("审核通过");
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("成功");
        jsonResult.setData(data);
        return jsonResult.getDataMap();
    }

    @ApiOperation(value = "获取所有可以选择的证书")
    @GetMapping("/getCertificates")
    public Map<String, Object> getCertificates() {
        List<Map<String, Object>> mapList = projectDetailsService.getCertificates();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("成功");
        jsonResult.setData(mapList);
        return jsonResult.getDataMap();
    }

    @ApiOperation(value = "根据类型id返回所有项目")
    @GetMapping("/getByType")
    @ApiImplicitParam(name = "tid", value = "tid", paramType = "query", dataType = "Long", dataTypeClass = Long.class)
    public Map<String, Object> getByType(@RequestParam(name = "tid") Long tid) throws BusinessException {
        List<ProjectDetails> byType = projectDetailsService.getByType(tid);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("成功");
        jsonResult.setData(byType);
        return jsonResult.getDataMap();
    }

    @ApiOperation(value = "根据项目id返回对应项目详情")
    @GetMapping("/getById")
    @ApiImplicitParam(name = "id", value = "id", paramType = "query", dataType = "Long", dataTypeClass = Long.class)
    public Map<String, Object> getById(@RequestParam(name = "id") Long id) throws BusinessException {
        ProjectDetails byType = projectDetailsService.getById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("成功");
        jsonResult.setData(byType);
        return jsonResult.getDataMap();
    }

}
